<template>
  <div>
    <div>**************************</div>
    <el-row class="mgtop10">
      <label style="margin-left: 10px">报告日期：</label>
      <el-date-picker v-model="form.startDate" style="width: 150px" @change="comDateChange(form.startDate)"
                      :clearable="false" placeholder="开始日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"/>
      <span>到</span>
      <el-date-picker v-model="form.endDate" style="width: 150px" @change="comDateChange(form.endDate)"
                      :clearable="false" placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd"/>
    </el-row>

    <el-button>按钮</el-button>
  </div>
</template>

<script>
import Date from "@/utils/date";

export default {
  name: "js-test",
  data(){
    return{
      joinObjData:{},
      tableForm:{
        geographicalTypeTableData:[]
      },
      form:{
        startDate:'2023-03-09',
        endDate:'2023-03-15'
      }
    }
  },
  methods:{
    comDateChange(val) {
      if (!val) return;
      // 周四-周三
      let one = new Date(val.replace(/-/g, "/"));
      let two = new Date(val.replace(/-/g, "/"));
      let oneDay = one.getDay();
      let twoDay = two.getDay();
      let aa = oneDay > 3 ? oneDay - 4 : oneDay + 3;
      let bb = twoDay < 4 ? 3 - twoDay : 10 - twoDay;
      one.setDate(one.getDate() - aa)
      two.setDate(two.getDate() + bb);
      let month = one.getMonth() + 1;
      let date = one.getDate();
      this.form.startDate = one.getFullYear() + '-' + (month > 10 ? month : '0' + month) + '-' + (date > 10 ? date : '0' + date);
      month = two.getMonth() + 1;
      date = two.getDate();
      this.form.endDate = two.getFullYear() + '-' + (month > 10 ? month : '0' + month) + '-' + (date > 10 ? date : '0' + date);
    },
    //已知年，获取该年的天数
    getYearAllDays(year) {
      let leapYear = false, sum_day = 0, month_arr = [4, 6, 9, 11];
      if (year % 100 === 0) { // 年份是整百
        leapYear = year % 400 === 0
      } else {
        leapYear = year % 4 === 0
      }
      // 下面计算每个月的天数
      for (let i = 1; i < 13; i++) {
        if (i === 2) {
          sum_day += leapYear ? 29 : 28
        } else if (month_arr.includes(i)) {
          sum_day += 30
        } else {
          sum_day += 31
        }
      }
      return sum_day
    },

    //已知年月,获取该月的天数
    getDays(years, mons) {
      //第一位是0去掉
      years = Number(years);
      if (mons.charAt(0) == '0') {
        mons = mons.substring(1, mons.length);
      }
      mons = Number(mons)
      let arr = []
      if (years % 4 == 0 && years % 100 != 0 || years % 400 == 0) {
        arr = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
      } else {
        arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
      }
      return arr[mons - 1]
    },
    //已知年和第几天获取 YYYY-MM-DD
    getYMDbyDays(year, dayNumInEveryYear) {
      let date = new Date(year + '-01-01');
      date.setDate(Number(dayNumInEveryYear))
      let mm = date.getMonth() + 1;
      mm = (mm < 10 ? '0' : '') + mm
      let dd = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return year + '-' + mm + "-" + dd;
    },
  }
}
</script>

<style scoped>
.mgtop10{margin-top: 10px}
</style>